package src.com.foreknow.demo13;

import java.util.Scanner;

public class Test {
    /**
     * 输出图书的信息
     */
    public void printBooksInfo(Book[] books){
        for (int i = 0;i < books.length;i++){
            Book book = books[i];//从数组中获取某一个Book对象
            System.out.println(book.getId()+"---"+book.getName()+"---"+book.getPrice()+"---"+book.getStorage());
        }
    }

    /**
     * 购买图书
     */
    public Order buyBooks(Book[] books){
        Scanner input = new Scanner(System.in);
        //创建一个订单对象
        Order order = new Order("0001");
        //循环三次
        for (int i = 0;i <3;i++){
            System.out.println("请输入图书的编号：");
            int pid = input.nextInt();
            System.out.println("请输入购买的数量：");
            int pnum = input.nextInt();
            //将接收到的数量保存到OrderItem对象中
            OrderItem orderItem = new OrderItem(books[pid-1].getName(),books[pid-1].getPrice(),pnum);
            //将OrderItem保存到数组当中
            order.setItems(orderItem,i);

        }
        return order;
    }

    /**
     * 输出订单的信息
     */
    public void printOrderInfo(Order order){
        //输出订单的信息
        System.out.println("订单的编号：" + order.getOrderId());
        System.out.println("订单总额：" + order.getTotal() );
        //获取到Order对象中的数组进行遍历
        OrderItem[] items = order.getItems();
        for(int i= 0; i<items.length;i++){
            OrderItem orderItem = items[i];
            System.out.println(orderItem.getPname()+"---"+orderItem.getPprice()+"---"+orderItem.getNum());
        }

    }

    public static void main(String[] args) {
        Test test = new Test();
        //创建数组容器用于保存图书对象
        Book[] books = new Book[3];
        //创建三个图书对象并给属性初始化 1. set方法 2. 构造器
        Book book1 = new Book(1,"java",33.45,50);
        Book book2 = new Book(2,"springboot",78.6,150);
        Book book3 = new Book(3,"css",45.3,250);
        books[0] = book1;
        books[1] = book2;
        books[2] = book3;
        //调用方法来输出图书的信息
        test.printBooksInfo(books);
        //调用购买图书的方法
        Order order = test.buyBooks(books);
        test.printOrderInfo(order);
    }
}











